package com.ja.openchat.viewmodels;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.ja.openchat.R;
import com.ja.openchat.ui.found.DynamicDetailsFragment;
import com.ja.openchat.ui.me.MyAlbumFragment;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;
import com.zy.openchat.core.model.user.UserInfo;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.MultiItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.binding.command.BindingConsumer;

public class GoChatCircleHotItemViewModel extends MultiItemViewModel<GoChatCircleHotViewModel> {
    public ObservableField<String> entity = new ObservableField<>();

    public ObservableField<UserInfo> userAvatar = new ObservableField<>(UserInfo.getUserInfo());
    List<Integer> datas = new ArrayList<>();

    public GoChatCircleHotItemViewModel(@NonNull GoChatCircleHotViewModel viewModel, String entity) {
        super(viewModel);
        this.entity.set(entity);
        datas.add(R.mipmap.zdy_1);
        datas.add(R.mipmap.zdy_2);
        datas.add(R.mipmap.zdy_3);
        datas.add(R.mipmap.zdy_4);
        datas.add(R.mipmap.zdy_5);
    }

    /**
     * 获取position的方式有很多种,indexOf是其中一种，常见的还有在Adapter中、ItemBinding.of回调里
     *
     * @return
     */
    public int getPosition() {
        return viewModel.getItemPosition(this);
    }

    //条目的点击事件
    public BindingCommand itemMoreClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            ToastUtils.showShort("itemMoreClick" + entity.get());
        }
    });
    //条目的点击事件
    public BindingCommand itemheaderMoreClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            viewModel.startContainerActivity(MyAlbumFragment.class.getCanonicalName());
        }
    });
    //条目的点击事件
    public BindingCommand itemClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            viewModel.startContainerActivity(DynamicDetailsFragment.class.getCanonicalName());
        }
    });
    //条目的长按事件
    public BindingCommand itemLongClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            //以前是使用Messenger发送事件，在NetWorkViewModel中完成删除逻辑
//            Messenger.getDefault().send(NetWorkItemViewModel.this, NetWorkViewModel.TOKEN_NETWORKVIEWMODEL_DELTE_ITEM);
            //现在ItemViewModel中存在ViewModel引用，可以直接拿到LiveData去做删除
//            ToastUtils.showShort(entity.get().getTypeContent());
        }
    });

    public BindingCommand<Banner> banner = new BindingCommand(new BindingConsumer<Banner>() {
        @Override
        public void call(Banner banner) {
            banner.setAdapter(new BannerImageAdapter<Integer>(datas) {
                @Override
                public void onBindView(BannerImageHolder holder, Integer data, int position, int size) {
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ToastUtils.showShort("点击图片" + position);
                        }
                    });
                    //图片加载自己实现
                    Glide.with(holder.itemView)
                            .load(data).centerCrop()
                            .into(holder.imageView);
                }
            }).setIndicator(new CircleIndicator(viewModel.getApplication()));

        }
    });
}
